package com.kws.merchant.service.impl;


import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.kws.merchant.domain.AfterSalesHistory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.AfterSalesMalfunction;
import com.kws.merchant.domain.AfterSalesWarranty;
import com.kws.merchant.mapper.AfterSalesHistoryMapper;
import com.kws.merchant.service.AfterSalesHistoryService;
import com.kws.merchant.service.AfterSalesMalfunctionService;
import com.kws.merchant.service.AfterSalesWarrantyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 售后-保修、故障单历史记录(AfterSalesHistory)表服务实现类
 *
 * @author yxy
 * @since 2024-10-23 14:19:54
 */
@Service("afterSalesHistoryService")
public class AfterSalesHistoryServiceImpl extends ServiceImpl<AfterSalesHistoryMapper, AfterSalesHistory> implements AfterSalesHistoryService {

    @Resource
    private AfterSalesHistoryMapper afterSalesHistoryMapper;

    /**
     * 分页查询列表
     */
    @Override
    public Page<AfterSalesHistory> pageList(AfterSalesHistory afterSalesHistory) {
        Page<AfterSalesHistory> page = new Page<>(0, 10);
        LambdaQueryWrapper<AfterSalesHistory> queryWrapper = Wrappers.<AfterSalesHistory>lambdaQuery();

        return this.page(page, queryWrapper);
    }

    /**
     * 删除售后单记录
     *
     * @param idList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeByAfterSalesNo(String type, List<Long> idList) {
        switch (type) {
            case "GZ":
                idList.forEach(x->{
                    AfterSalesMalfunction afterSalesMalfunction = SpringUtil.getBean(AfterSalesMalfunctionService.class).getById(x);
                    if (ObjectUtils.isEmpty(afterSalesMalfunction)){
                        return;
                    }
                    String malfunctionNo = afterSalesMalfunction.getMalfunctionNo();
                    this.removeByAfterNo(malfunctionNo);
                });
                break;
            case "BX":
                idList.forEach(x->{
                    AfterSalesWarranty afterSalesWarranty = SpringUtil.getBean(AfterSalesWarrantyService.class).getById(x);
                    if (ObjectUtils.isEmpty(afterSalesWarranty)){
                        return;
                    }
                    String malfunctionNo = afterSalesWarranty.getWarrantyNo();
                    this.removeByAfterNo(malfunctionNo);
                });
                break;
        }

    }

    /**
     * 删除单号记录
     * @param afterSalesNo
     */
    @Override
    public void removeByAfterNo(String afterSalesNo) {
        this.remove(Wrappers.<AfterSalesHistory>lambdaQuery().eq(AfterSalesHistory::getAfterSalesNo,afterSalesNo));
    }

    @Override
    public List<AfterSalesHistory> listByWarrantyNo(String warrantyNo) {
        List<AfterSalesHistory> list = this.list(Wrappers.<AfterSalesHistory>lambdaQuery().eq(AfterSalesHistory::getAfterSalesNo, warrantyNo));
        return list;
    }


}
